<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="设备ID" prop="equipmentId">
        <el-input v-model="queryParams.equipmentId" placeholder="请输入设备ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="预计完成时间" prop="expectedCompletionTime">
        <el-date-picker clearable v-model="queryParams.expectedCompletionTime" type="date"
          value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择预计完成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="完成情况" prop="completionStatus">
        <el-select v-model="queryParams.completionStatus" placeholder="请选择完成情况" clearable filterable>
          <el-option v-for="dict in dict.type.performance" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否是信息变更" prop="isInfoChange">
        <el-input v-model="queryParams.isInfoChange" placeholder="请输入是否是信息变更" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['equipment:repairHistory:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['equipment:repairHistory:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['equipment:repairHistory:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['equipment:repairHistory:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repairHistoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="设备ID" align="center" prop="equipmentId" />
      <el-table-column label="状态描述" align="center" prop="statusDescription" />
      <el-table-column label="状态描述及处理" align="center" prop="statusDescriptionAction">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.status_description_handing_situation"
            :value="scope.row.statusDescriptionAction" />
        </template>
      </el-table-column>
      <el-table-column label="设备维修问题点" align="center" prop="repairIssues" />
      <el-table-column label="维修记录" align="center" prop="repairRecords" />
      <el-table-column label="预计完成时间" align="center" prop="expectedCompletionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expectedCompletionTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成情况" align="center" prop="completionStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.performance" :value="scope.row.completionStatus" />
        </template>
      </el-table-column>
      <el-table-column label="是否是信息变更" align="center" prop="isInfoChange" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['equipment:repairHistory:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['equipment:repairHistory:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改维修历史记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备ID" prop="equipmentId">
          <el-input v-model="form.equipmentId" placeholder="请输入设备ID" />
        </el-form-item>
        <el-form-item label="状态描述" prop="statusDescription">
          <el-input v-model="form.statusDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="状态描述及处理" prop="statusDescriptionAction">
          <el-input v-model="form.statusDescriptionAction" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="设备维修问题点" prop="repairIssues">
          <el-input v-model="form.repairIssues" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="维修记录" prop="repairRecords">
          <el-input v-model="form.repairRecords" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="预计完成时间" prop="expectedCompletionTime">
          <el-date-picker clearable v-model="form.expectedCompletionTime" type="date" value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择预计完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="完成情况">
          <el-radio-group v-model="form.completionStatus">
            <el-radio v-for="dict in dict.type.performance" :key="dict.value" :label="parseInt(dict.value)">{{
              dict.label
            }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否信息变更" prop="isInfoChange">
          <el-input v-model="form.isInfoChange" placeholder="请输入是否是信息变更" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRepairHistory, getRepairHistory, delRepairHistory, addRepairHistory, updateRepairHistory } from "@/api/equipment/repairHistory";

export default {
  name: "RepairHistory",
  dicts: ['performance'],
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维修历史记录表格数据
      repairHistoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentId: null,
        statusDescription: null,
        statusDescriptionAction: null,
        repairIssues: null,
        repairRecords: null,
        expectedCompletionTime: null,
        completionStatus: null,
        isInfoChange: null,
        remark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键不能为空", trigger: "blur" }
        ],
        equipmentId: [
          { required: true, message: "设备ID不能为空", trigger: "blur" }
        ],
        statusDescription: [
          { required: true, message: "状态描述不能为空", trigger: "blur" }
        ],
        statusDescriptionAction: [
          { required: true, message: "状态描述及处理不能为空", trigger: "blur" }
        ],
        repairIssues: [
          { required: true, message: "设备维修问题点不能为空", trigger: "blur" }
        ],
        repairRecords: [
          { required: true, message: "维修记录不能为空", trigger: "blur" }
        ],
        expectedCompletionTime: [
          { required: true, message: "预计完成时间不能为空", trigger: "blur" }
        ],
        completionStatus: [
          { required: true, message: "完成情况不能为空", trigger: "blur" }
        ],
        isInfoChange: [
          { required: true, message: "是否是信息变更不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询维修历史记录列表 */
    getList() {
      this.loading = true;
      listRepairHistory(this.queryParams).then(response => {
        this.repairHistoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        equipmentId: null,
        statusDescription: null,
        statusDescriptionAction: null,
        repairIssues: null,
        repairRecords: null,
        expectedCompletionTime: null,
        completionStatus: 0,
        isInfoChange: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加维修历史记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRepairHistory(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改维修历史记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRepairHistory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRepairHistory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除维修历史记录编号为"' + ids + '"的数据项？').then(function () {
        return delRepairHistory(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('equipment/repairHistory/export', {
        ...this.queryParams
      }, `repairHistory_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
